﻿#include "XMLTools.h"
#include "ScintillaDoc.h"
#include "ToolsPrettyPrintFast.h"
#include "setting.h"

#include <functional>
#include <qsciscintilla.h>
#include <Scintilla.h>

extern QWidget* s_pMainNotepad;
extern std::function<QsciScintilla* ()> s_getCurEdit;
XmltoolsOptions xmltoosOptions;

void nddLinearizeXmlFast() 
{
    QsciScintilla* pEdit = s_getCurEdit();
    if (pEdit == nullptr)
    {
        return;
    }

    ScintillaDoc doc = ScintillaDoc(pEdit);

    if (xmltoosOptions.formatingEngine == 0) {
        sciDocLinearizeQuickXml(doc);
    }
    else if (xmltoosOptions.formatingEngine == 1) {
       sciDocLinearizeStringXml(doc);
    }
    else {
        sciDocLinearizeSimpleXml(doc);
    }

    doc.SetXOffset(0);
}


void nddPrettyPrintXmlIndentOnlyFast()
{
    QsciScintilla* pEdit = s_getCurEdit();
    if (pEdit == nullptr)
    {
        return;
    }

    ScintillaDoc doc = ScintillaDoc(pEdit);

    if (xmltoosOptions.formatingEngine == 0) {
        sciDocPrettyPrintQuickXml_IndentOnly(doc);
    }
    else if (xmltoosOptions.formatingEngine == 1) {
        sciDocPrettyPrintStringXml_IndentOnly(doc);
    }
    else {
        sciDocPrettyPrintSimpleXml_IndentOnly(doc);
    }

    doc.SetXOffset(0);
}

void nddPrettyPrintXmlFast()
{
    QsciScintilla* pEdit = s_getCurEdit();
    if (pEdit == nullptr)
    {
        return;
    }

    ScintillaDoc doc = ScintillaDoc(pEdit);

    if (xmltoosOptions.formatingEngine == 0) {
        sciDocPrettyPrintQuickXml(doc);
    }
    else if (xmltoosOptions.formatingEngine == 1) {
        sciDocPrettyPrintStringXml(doc);
    }
    else {
        sciDocPrettyPrintSimpleXml(doc);
    }

    doc.SetXOffset(0);
}

void nddPrettyPrintXmlAttrFast()
{
    QsciScintilla* pEdit = s_getCurEdit();
    if (pEdit == nullptr)
    {
        return;
    }

    ScintillaDoc doc = ScintillaDoc(pEdit);

    if (xmltoosOptions.formatingEngine == 0) {
        sciDocPrettyPrintQuickXmlAttr(doc);
    }
    else if (xmltoosOptions.formatingEngine == 1) {
        sciDocPrettyPrintStringXmlAttr(doc);
    }
    else {
        sciDocPrettyPrintSimpleXmlAttr(doc);
    }

    doc.SetXOffset(0);
}

void optionsWin()
{
    Setting* pWin = new Setting(s_pMainNotepad);
    pWin->setAttribute(Qt::WA_DeleteOnClose);
    pWin->setWindowFlag(Qt::Window);
    pWin->show();
}

//加载参数，从文件中读取配置参数。
void loadOption()
{
    Setting::readSet(true);
}